/*
 *  Copyright 2009 Rohan Ranade.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.netbeans.modules.cpextender.ui.customizer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import org.netbeans.api.project.Project;
import org.netbeans.modules.cpextender.api.CPExtensionManager;
import org.netbeans.modules.cpextender.util.Utilities;
import org.netbeans.spi.project.ui.support.ProjectCustomizer;
import org.netbeans.spi.project.ui.support.ProjectCustomizer.Category;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

/**
 * Provides a category in the project properties panel for specifying/editing
 * the project classpath extension file
 *
 * @author Rohan Ranade (Rohan.Ranade@Sun.COM)
 */
public class ExtensionCategoryProvider implements ProjectCustomizer.CompositeCategoryProvider {

    public static final String CATEGORY_NAME = "CPExtender";

    public Category createCategory(Lookup ctx) {
        Project p = Utilities.getProject(ctx);
        // TODO do some munging over the project perhaps
        String categoryName = NbBundle.getMessage(ExtensionCategoryProvider.class, "LBL_ExtnProvider");
        return Category.create(CATEGORY_NAME, categoryName, null);
    }

    public JComponent createComponent(Category category, Lookup ctx) {
        Project p = Utilities.getProject(ctx);
        CPExtensionManager manager = Utilities.getCPExtensionManager(p);

        CPExtensionPanel panel = new CPExtensionPanel(manager.isExtendedProject(), manager.getExtensionFilePath());
        CategoryListener listener = new CategoryListener(manager, panel);
        category.setOkButtonListener(listener);
        category.setStoreListener(listener);
        return panel;
    }

    private static final class CategoryListener implements ActionListener {

        private final CPExtensionPanel panel;
        private final CPExtensionManager manager;
        private volatile Boolean isEnabled;
        private volatile String filePath;

        public CategoryListener(CPExtensionManager manager, CPExtensionPanel panel) {
            this.manager = manager;
            this.panel = panel;
        }

        public void actionPerformed(ActionEvent e) {
            if (isEnabled == null && filePath == null) {
                isEnabled = panel.isExtensionEnabled();
                filePath = panel.getExtensionFilePath();
            } else {
                // store listener - TODO
                if (!isEnabled) {
                    filePath = null;
                }
                manager.setExtensionFilePath(filePath);
            }
        }

    }
}